package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionMapper;
import com.tanhua.server.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
    查询数据中的问题
 */
@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    /**
     * 查询问题
     *
     * @param userId
     * @return
     */
    public Question queryQuestion(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }


    /**
     * 设置陌生人问题
     *
     * @param userId
     * @param content
     */
    public void save(Long userId, String content) {
        //查询问题是否存在
        Question question = this.queryQuestion(userId);
        //存在更改
        if (null != question) {
            question.setTxt(content);
            questionMapper.updateById(question);
        } else {
            //不存在,新增
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            this.questionMapper.insert(question);
        }


    }
}
